﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System;
using MikuMikuModel.FarcPack;
using MikuMikuModel.Logs;
using System.Runtime.InteropServices;
using System.Globalization;

namespace ft_module_parser
{
    class Program
    {
        public static string ayy = "";
        public static void Main(string[] args)
        {
            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
            Console.InputEncoding = System.Text.Encoding.Unicode;
            Console.OutputEncoding = System.Text.Encoding.Unicode;

            //System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.Idle;

            Logs.Initialize();

            //XPvParam.Process();
            //return;
            //pvs FT_PV = new pvs();
            //FT_PV.ParseFT("pv_db.txt");
            //FT_PV.SaveToJson();
            //FT_PV.SaveToA("pv_db_ac.txt");

            //pv_lists FT_PVList = new pv_lists();
            //FT_PVList.ParseFT("gm_pv_list.bin");
            //FT_PVList.SaveToJson();
            //FT_PVList.SaveToA("gm_pv_list_ac.txt");

            //pvfield_filter pvfield = new pvfield_filter("pv_field.txt", "pv_field_ac.txt");
            //pvfield.filter();

            //a3ds a3d = new a3ds();
            //a3d.MergeRaki(); now Merge2
            //a3d.Merge();
            //a3d.FixMissing();

            /*
            a3dfixer a3dhrc2 = new a3dfixer();
            a3dhrc2.fixhrc(@"P:\a3daa", @"C:\dl\Compressed\MikuMikuLibrary-master\MikuMikuLibrary-master\MikuMikuModel\bin\released_bins\mikumikumodel_ft_to_ac_050419\alt_db\pv_db.txt");
            a3dhrc2.serialize(@"P:\a3daa");

            //a3dhrc = new a3dfixer();
            try
            {
                a3dfixer a3dhrc = new a3dfixer();
                if (args.Count() == 0)
                {
                    if (File.Exists(@"../alt_db/pv_db_orig")) a3dhrc.fixhrc2(@"../alt_db/pv_db_orig");
                    else
                    {
                        System.Console.WriteLine("args[0] = original pv database");
                    }
                }
                else a3dhrc.fixhrc2(args[0]);
            } catch
            {
                System.Console.WriteLine("ERROR");
            }

            */

            //osages ospd = new osages();
            //ospd.Merge(@"D:\pdiva\rom\osage_play_data\opd_db.txt", @"F:\ACFT\rom\osage_play_data\opd_db.txt");
            //ospd.Merge(@"P:\pdiva_test\rom\osage_play_data\opd_db.txt", @"F:\ACFT\rom\osage_play_data\mdata_opd_db.txt");

#if divamods
            divapvmod divapvmods = new divapvmod();
            if (args.Count() == 0)
                divapvmods.ApplyMods();
            else if (args[0] == "-restore")
            {
                divapvmods.RestoreDb();
            } else divapvmods.ApplyMods();
#endif

            //test stuff
            //pv_lists_raw test = new pv_lists_raw();
            //test.load(@"F:\dl\gm_pv_list_2.bin");

            //diva x objset generate

            //Tools.MassPackFolders("a3d", @"D:\pdiva\rom\auth_3d\");


#if X2AC
            pdaconversion.divax.mass_convert divax = new pdaconversion.divax.mass_convert();
            if (args.Count() == 0)
            {
                Console.WriteLine("no args");
            } else if (args.Count() == 7)
            {
                divax.Convert(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
            }
#endif

            //divax.Convert(@"F:\divax\data\PCSB01007\rom\data\", @"P:\pdiva_test\rom\objset\obj_db.bin", @"P:\pdiva_test\rom\objset\tex_db.bin", @"P:\pdiva_test\rom\stage_data.bin", @"P:\pdiva_test\rom\auth_3d\auth_3d_db.bin", @"P:\pdiva_test\rom\rob\mot_db.farc", @"P:\pdiva_test");
            //Tools.MassPackFolders(@"D:\pdiva" + "\\rom\\rob_temp", @"D:\pdiva" + "\\rom\\rob\\");

#if FT2AC
            var ftdx = new pdaconversion.ftdx.mass_convert();
            ftdx.doConvert();
#endif

#if (RELEASE || DEBUG)
            Console.OutputEncoding = System.Text.Encoding.UTF8;

            if (System.AppDomain.CurrentDomain.FriendlyName == "DivaScriptConv.exe")
            {
                DivaScriptConv.DSC.Convert(args);
                return;
            }

            bool check = false;
            string appfilename = System.AppDomain.CurrentDomain.FriendlyName;
            if (args.Count() != 0)
            {
                if (args[0] == "-restore")
                {
                    divapvmod divapvmods = new divapvmod();
                    divapvmods.RestoreDb();
                    check = true;
                } else
                {
                    var cargs = args.ToList();
                    appfilename = cargs.First();
                    cargs.RemoveAt(0);
                    args = cargs.ToArray();
                }
            }

            if (!check)
            {
                if (appfilename == "divamods.exe")
                {
                    divapvmod divapvmods = new divapvmod();
                    divapvmods.ApplyMods();
                }
                else if (appfilename == "divamods_f.exe")
                {
                    divapvmod divapvmods = new divapvmod();
                    divapvmods.ApplyMods(0, "", false, true);
                }
                else if (appfilename == "divamods_aft.exe")
                {
                    rescb.InitResc();
                    if (args.Count() == 1)
                        if (args[0] == "-ppd")
                            aftconversion.ppd.from_config.do_convert();
                    divapvmod divapvmods = new divapvmod();
                    divapvmods.ApplyMods(1);
                }
                else if (appfilename == "x_to_ac.exe")
                {
                    pdaconversion.divax.from_config.doConvert();
                }
                else if (appfilename == "ft_to_ac.exe")
                {
                    var ftdx = new pdaconversion.ftdx.mass_convert();
                    ftdx.doConvert();
                    Console.ReadKey();
                }
                else if (appfilename == "sprselpv.exe")
                {
                    rescb.InitResc();
                    sprutilities.sprselpv.Make();
                }
                else if (appfilename == "ppd2dsc.exe")
                {
                    PPD2DSC ppd = new PPD2DSC();
                    ppd.Convert(args.ToArray());
                    Console.WriteLine("Done");
                    Console.ReadKey();
                }
                else if (appfilename == "ppd2divamods.exe")
                {
                    rescb.InitResc();
                    if (args.Count() == 1)
                        if (args[0] == "-ppd")
                            aftconversion.ppd.from_config.do_convert();
                    Console.ReadKey();
                }
                else if (appfilename == "x_to_aft.exe")
                {
                    aftconversion.divax.from_config.doConvert();
                }
                else if (appfilename == "x_to_f.exe")
                {
                    fconversion.divax.from_config.doConvert();
                }
                else if (appfilename == "f_to_ac.exe")
                {
                    throw new NotImplementedException(Program.ayy);
                }
                else if (appfilename == "f_to_aft.exe")
                {
                    throw new NotImplementedException(Program.ayy);
                }
                else if (appfilename == "f2nd_to_ac.exe")
                {
                    throw new NotImplementedException(Program.ayy);
                }
                else if (appfilename == "f2nd_to_aft.exe")
                {
                    throw new NotImplementedException(Program.ayy);
                }
                else if (appfilename == "shaderaft.exe")
                {
                    ShaderAFT.Process(args);
                }
                else if (appfilename == "shaderaft_amd.exe")
                {
                    ShaderAFT.Process(args, true);
                }
                else if (appfilename == "shdmodify.exe")
                {
                    ObjModify.ChgShader(args);
                }
                else if (appfilename == "bc5begone.exe")
                {
                    BC5Begone.Process(args);
                }
                else if (appfilename == "dbgdsc")
                {
                    ToolsExperiment.DSCtest.DumpPVScript(args);
                }
                else
                {
                    Console.WriteLine("ft_parser");
                    Console.WriteLine(textconst.credits);
                    Console.WriteLine("");
                    Console.WriteLine("Valid EXE Names (Rename ft_parser.exe into these below to function)");
                    Console.WriteLine("");
                    Console.WriteLine("divamods.exe");
                    Console.WriteLine("divamods_aft.exe");
                    Console.WriteLine("  Parameters:");
                    Console.WriteLine("  -ppd : Looks for ppd folder and tries to convert to Divamods format. (AFT Only)");
                    Console.WriteLine("  -restore : Restores stuff.");
                    Console.WriteLine("");
                    Console.WriteLine("x_to_ac.exe");
                    Console.WriteLine("x_to_aft.exe");
                    Console.WriteLine("ft_to_ac.exe");
                    Console.WriteLine("DivaScriptConv.exe");
                    Console.WriteLine("sprselpv.exe");
                    Console.ReadKey();
                }
            }

#endif


#if PRIVATE
            pdaconversion.divax.mass_convert divax = new pdaconversion.divax.mass_convert();
            divax.Convert(@"F:\divax\data\PCSB01007\rom\data\", @"D:\DL\obj_db.bin", @"D:\DL\tex_db.bin", @"D:\DL\stage_data.bin", @"D:\DL\auth_3d_db.bin", @"D:\DL\mot_db.farc", @"D:\pdiva");
#endif

            //ftconversion.ftdx.diff_tool.DoDiffTxt(@"F:\ACFT\rom", @"P:\FTDX\rom");

            //System.Console.ReadKey();
        }
    }
}
